/*
 * $Id: User.java 12 2014-08-10 04:00:19Z ratking $
 *
 * Copyleft (C) 2014 RatKing. All wrongs reserved.
 */
package cn.ratking.demo.jaws6.model;

import java.io.Serializable;
import java.util.Date;
import java.util.Objects;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * 用户
 */
@Entity
@Table(name = "T_User",
        uniqueConstraints = {
            @UniqueConstraint(columnNames = "username")
        })
public class User implements Serializable {

    /** 用户ID */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long userId;
    /** 用户名(不得重复) */
    @NotNull
    @Size(min = 1, max = 20)
    @Column(unique = true)
    private String username;
    /** 口令(密文) */
    @NotNull
    @Size(max = 512)
    private String password;
    /** 真实姓名 */
    @NotNull
    @Size(min = 1, max = 20)
    private String realName;
    /** 性别(GB2261 人的性别代码：0-未知的性别 1-男性 2-女性) */
    @Size(max = 20)
    private String sex;
    /** 生日 */
    @Temporal(TemporalType.DATE)
    private Date birthday;
    /** 消息摘要算法(MD5、SHA-1、SHA-256) */
    @Size(max = 20)
    private String algorithm;
    /** 盐 */
    @Size(max = 512)
    private String salt;
    /** 是否启用(false/0-禁用 true/1-启用) */
    private boolean enabled;
    /** 备注 */
    @Size(max = 1000)
    private String remark;
    /** 创建时间 */
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;
    /** 更新时间 */
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;
    /** 版本(用于JPA乐观锁) */
    @Version
    private int version;

    /** 无参构造方法 */
    public User() {
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getAlgorithm() {
        return algorithm;
    }

    public void setAlgorithm(String algorithm) {
        this.algorithm = algorithm;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(userId);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof User)) {
            return false;
        }
        final User other = (User) obj;
        return Objects.equals(this.userId, other.userId);
    }

    /**
     * 返回测试字符串
     *
     * @return 测试字符串
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        sb.append("[User: userId=").append(userId);
        sb.append(", username=").append(username);
        sb.append(", password=").append(password);
        sb.append(", realName=").append(realName);
        sb.append(", sex=").append(sex);
        sb.append(", birthday=").append(birthday);
        sb.append(", algorithm=").append(algorithm);
        sb.append(", salt=").append(salt);
        sb.append(", enabled=").append(enabled);
        sb.append(", remark=").append(remark);
        sb.append(", createTime=").append(createTime);
        sb.append(", updateTime=").append(updateTime);
        sb.append(", version=").append(version);
        sb.append("]");

        return sb.toString();
    }
}
